<?php
class indexController extends Controller {
	
	public $table = 'page';
   
	public function initialize() {
	    $this->view->title = '';
		$this->view->layoutPath = 'template';
		$this->view->templateUrl = 'template';
		$this->view->setLayout = 'default';

	
    }
    
    //--------------- Begin index function -----------------//
    
    public function indexAction() {
        $this->view->title = 'PG Schagen - Reserveringsmodule';
		$this->view->add_js('jquery-min');
		$this->view->add_js('jquery-ui-min');
		
		if(isset($_GET['reserveer'])){
			$this->addClient();
			$this->addReservation();
		}
		
		$this->getRooms();
		$this->getReservations();
    	$this->view->show();
    	
    }
	
	public function getRooms(){
		$rooms = DBH::getInstance()->read_all("room");
		$this->view->rooms = $rooms;
	}
	
	public function getReservations(){
		$reservations = DBH::getInstance()->read_all("booking");
		$this->view->reservations = $reservations;
	}
	
	public function addClient(){
		
		$object = new stdClass();

			$object->name = $_POST['naam'];
			$object->email = $_POST['email'];
			$object->address = $_POST['adres'];
			$object->postalcode = $_POST['zipcode'];
			$object->phonenumber = $_POST['telnr'];
			//TODO add userid in object
			if(!DBH::getInstance()->create($object, "client")){
				return false;
			}
			return true;	
	}	
	
	public function addReservation(){
		$sql = "name = '".$_POST['naam']."' AND email = '".$_POST['email']."'";
			$resultaat = DBH::getInstance()->read($sql, "client");
		
		$date = new DateTime($_POST['date']);
		
		$object = new stdClass();

			$object->room_id = $_POST['room_id'];
			$object->date = $date->format('Y-m-d');
			$object->partofday = $_POST['partofday'];
			$object->client_id = $resultaat->id;
			//TODO add userid in object
			if(!DBH::getInstance()->create($object, "booking")){
				return false;
			}
			return true;	
	}	
}
